<?php
/**
 * Created by PhpStorm.
 * User: KaZeline
 * Date: 15-5-18
 * Time: 上午9:25
 * 查询IP地址的接口 用来修改联盟内的邀请人
 */
//fopen('/var/www/html/new/d/module/zax/ip/error.log','w+');
function ip_inviterID($inviterID){//参数是帐号account
//account->id.
//    echo 'account:'.$inviterID;
$origin_inviterID = $inviterID;
$sql = "select id from pft_member where account='$inviterID' limit 1";
$GLOBALS['le']->query($sql);
$GLOBALS['le']->fetch_assoc();
$inviterID = $GLOBALS['le']->f('id');
if(!$inviterID) goto no_change_inviterID;
$origin_fid = $inviterID;
//    echo 'id:'.$inviterID;exit;
$sql = "select fid from pft_distributor_union_SE where fid=$inviterID limit 1";
$GLOBALS['le']->query($sql);
$row=$GLOBALS['le']->fetch_assoc();
if(!$row['fid']) goto no_change_inviterID;

//$ip = '115.156.238.114';
$j = file_get_contents('http://ip.taobao.com/service/getIpInfo.php?ip='.$_SERVER['REMOTE_ADDR']);
$j = json_decode($j,true);
//print_r($j);exit;
//找地区编码
$sql = "select n_code,parent_province from pft_area_code_concat where city='{$j['data']['city']}' limit 1";
$GLOBALS['le']->query($sql);
$area=$GLOBALS['le']->fetch_assoc();
if(!$area['n_code']){
    $content = date('Y-m-d H:i:s').'##没有找到该地区编码:'.$j['data']['region'].$j['data']['city'].',ip地址为'.$_SERVER['REMOTE_ADDR'].'\r\n';
    file_put_contents('/var/www/html/new/d/module/zax/ip/error.log',$content);
    goto no_change_inviterID;
}
//找市级代理商
$sql = "select inviterID from pft_distributor_agency where darea_code={$area['n_code']} and fid=$inviterID limit 1";
//    echo $sql;
$GLOBALS['le']->query($sql);
$row=$GLOBALS['le']->fetch_assoc();
//    print_r($row);
//    exit;
if(!$row['inviterID'] && $area['n_code']!=$area['parent_province'])
//找不到市级代理商就去找省级代理商 直辖市不查 省市同级
{
    $sql = "select inviterID from pft_distributor_agency where darea_code={$area['parent_province']} and fid=$inviterID limit 1";
    $GLOBALS['le']->query($sql);
    $row=$GLOBALS['le']->fetch_assoc();
}
$inviterID = $row['inviterID'] ? $row['inviterID'] : $inviterID;
no_change_inviterID:
//id->account
if($origin_fid!=$inviterID){
    $sql = "select account from pft_member where id='$inviterID' limit 1";
    $GLOBALS['le']->query($sql);
    $GLOBALS['le']->fetch_assoc();
    $inviterID = $GLOBALS['le']->f('account');
    return $inviterID;
}
return $origin_inviterID;

}
//include_once'/var/www/html/new/com.inc.php';
//$i = ip_inviterID(124293); //27073  27075 6610   124408  124293随便列了几个盟主帐号 留测试用
//echo $i;